Flow Control
if
Statementβ
In addition to storing information, variables are useful for controlling what is shown to the player. To achieve this, you use if
statements.
An if
statement allows you to control whether a block of content will be displayed or not. You specify a condition that is checked, and if it evaluates to "true", all the lines between the <<if>>
and <<endif>>
statements are executed.
For example, consider the following code:
<<set $gold_amount = 5>>
I want to buy a pie!
<<if $gold_amount < 10>>
But I don't have enough money!
<<endif>>
In this example, the variable $gold_amount
is set to 5. The line "I want to buy a pie!" is shown, and then it checks whether $gold_amount
is less than 10. If it is (which in this case it is), the line "But I don't have enough money!" will be displayed.
elseif
and else
Statementsβ
You can use elseif
and else
statements to handle different cases in an if
statement.
An elseif
statement checks a condition if the if
statement and any previous elseif
statements did not run.
An else
statement does not have a condition and runs if none of the previous conditions are met.
Example:
I want to buy a pie!
<<if $gold_amount < 10>>
But I don't have enough money!
<<elseif $gold_amount < 15>>
I need to save a little more!
<<else>>
And I'll buy it!
<<endif>>
This script will display different lines based on the value of $gold_amount
. The conditions are evaluated from top to bottom, meaning that for an elseif
or else
to run, all previous conditions must have failed.
- If it's less than 10, the line "But I don't have enough money!" will run.
- If it's less than 15 but greater than or equal to 10, the line "I need to save a little more!" will run.
- Otherwise, the line "And I'll buy it!" will run.
The condition used in if
and elseif
statements must return a boolean value (i.e., true or false). For example, <<if 1>>
is not allowed, but <<if 1 == 1>>
is. You also cannot directly use variables in conditions, even if they are boolean, such as <<if $isTrue>>
, where $isTrue
is a boolean variable. Instead, use <<if $isTrue == true>>
.
Conditional Responsesβ
When providing response options to the player, you may want to make some options unavailable. You can achieve this by adding a condition to the response.
For example, if you have a variable tracking the player's "reputation points" called $reputation
, you can make certain responses available only if the player's reputation is high enough.
You add the condition to the response using an if
statement. Example:
You're not allowed in!
-> Of course I am! The boss knows me! <<if $reputation > 10>>
-> Please?
When t-Plot processes this set of responses, it checks the condition in the if
statement. If the condition evaluates to false
, the player will not see that response.